/**
 * Copyright (C) 2015-2018 IQIYI
 * All rights reserved.
 *
 * Author     : 王海斌
 * E-mail     : wanghaibin@qiyi.com
 * Version    :
 * Date       : Wed 17 Oct 2018 03:38:25 PM CST
 * Destription: 测试申请大内存后RSS在glibc2.17版本不归还操作系统
 *
 */

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>

#define LSIZE (2*65536+4096)
#define SSIZE 556
#define NL 500
#define NS 70000

int main(int argc, char **argv)
{
    void * bigalloc[NL];
    void * smallalloc[NS];
    int i;

    memset(bigalloc, 0, sizeof(bigalloc));
    memset(smallalloc, 0, sizeof(smallalloc));

    for (i = 0; i < (16*1024*1024*1024ULL)/65536; i++) {
        free(bigalloc[i % NL]);
        free(smallalloc[i % NS]);
        smallalloc[i % NS] = malloc(SSIZE);
        bigalloc[i % NL] = memalign(4096, LSIZE);
        memset(smallalloc[i % NS], 0, SSIZE);
        memset(bigalloc[i % NL], 0, LSIZE);

    }
    malloc_stats();

    system("ps axv|fgrep stressalloc");
}
